image: debian/bookworm
environment:
  #LABEL: "v0.3.0"
  LABEL: master
  TARGETDIR: "/home/public/ci.lrdu.org/android"
  REPO: transito

secrets:
  - 'a762687d-f65a-42d7-ad56-c2bd1edbcf94'
  - '47e261d2-a744-4a45-bda0-591baa3d381b'

tasks:
  - check-branch: |
     cd "$REPO"
     if [ "$(git rev-parse master)" != "$(git rev-parse HEAD)" ]; then \
        complete-build; \
     fi
  - prep: |
      sudo apt -y install default-jre sdkmanager golang openjdk-17-jdk-headless
      export ANDROID_HOME=/opt/android-sdk
      sudo mkdir -p $ANDROID_HOME
      sudo chown $USER $ANDROID_HOME
      sh -c 'yes | sdkmanager --licenses'
      sdkmanager "emulator" "cmdline-tools;latest" "platforms;android-31" "platform-tools"
      $ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager ndk-bundle "build-tools;31.0.0"
      go install 'gioui.org/cmd/gogio@latest'
  - buildapk: |
      export ANDROID_SDK_ROOT=/opt/android-sdk
      export PATH="$PATH:/home/$USER/go/bin"
      cd "$REPO"
      ./build.sh bandroid
  - upload_htaccess: |
      printf %b 'Options +Indexes\nIndexOrderDefault Ascending Date\nIndexOptions NameWidth=*' > htaccess
      scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null htaccess $(cat ~/.user_at_server):/home/public/ci.lrdu.org/.htaccess
  - upload_image: |
      export TIME="$(date +%Y%m%d)"
      export GITSHA="$(cd transito; git rev-parse --verify HEAD | head -c 8)"
      export FILE="transito_${LABEL}_${GITSHA}-$TIME.apk"
      md5sum /home/build/transito/transito.apk > /home/build/sum.md5
      cat /home/build/sum.md5
      ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $(cat ~/.user_at_server) "rm -f ${TARGETDIR}/transito_${LABEL}_*"
      ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $(cat ~/.user_at_server) "mkdir -p $TARGETDIR"
      scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null /home/build/transito/transito.apk "$(cat ~/.user_at_server):${TARGETDIR}/$FILE"
      scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null /home/build/sum.md5 "$(cat ~/.user_at_server):${TARGETDIR}/$FILE.md5"